package com.funny.learning.english.util;

import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;

public class PlayMusic {
	private MediaPlayer mMediaPlayer;
	
	private Context context;
	
	public PlayMusic(Context context) {
		this.context = context;
	}

    public void stop() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    public void play(String srcSound) {
        stop();
        
//        int idx = new Random().nextInt(SoundConfig.arrSound.length);
//		int sRandom = (SoundConfig.arrSound[idx]);
        
//        mMediaPlayer = MediaPlayer.create(context, srcSound);
        mMediaPlayer = new MediaPlayer();
        try {
        	if(srcSound != null){
        		mMediaPlayer.setDataSource(srcSound);
    			mMediaPlayer.setVolume(0.4f, 0.4f);
    	        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    	            @Override
    	            public void onCompletion(MediaPlayer mediaPlayer) {
    	                stop();
    	            }
    	        });
    	        mMediaPlayer.prepare();
    	        mMediaPlayer.start();
        	}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
        
    }

	public MediaPlayer getmMediaPlayer() {
		return mMediaPlayer;
	}

	public void setmMediaPlayer(MediaPlayer mMediaPlayer) {
		this.mMediaPlayer = mMediaPlayer;
	}
    
    
}
